//
//  TextFieldDelegates.m
//  Honda
//
//  Created by Wang.Xinlei on 4/25/13.
//  Copyright (c) 2013 HeninHoo. All rights reserved.
//

#import "TextFieldDelegates.h"

@implementation TextFieldDelegate

+ (id)instance {
    return [[[self alloc] init] autorelease];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if ([self.delegate respondsToSelector:@selector(textFieldShouldBeginEditing:)]) {
        return [self.delegate textFieldShouldBeginEditing:textField];
    }
    
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if ([self.delegate respondsToSelector:@selector(textFieldDidBeginEditing:)]) {
        [self.delegate textFieldDidBeginEditing:textField];
    }
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if ([self.delegate respondsToSelector:@selector(textFieldShouldEndEditing:)]) {
        return [self.delegate textFieldShouldEndEditing:textField];
    }
    
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([self.delegate respondsToSelector:@selector(textFieldDidEndEditing:)]) {
        [self.delegate textFieldDidEndEditing:textField];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([self.delegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
        return [self.delegate textField:textField shouldChangeCharactersInRange:range replacementString:string];
    }
    
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    if ([self.delegate respondsToSelector:@selector(textFieldShouldClear:)]) {
        return [self.delegate textFieldShouldClear:textField];
    }
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if ([self.delegate respondsToSelector:@selector(textFieldShouldReturn:)]) {
        return [self.delegate textFieldShouldReturn:textField];
    }
    
    return YES;
}

@end



@implementation NameFieldDelegate

@end



@implementation NumberFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return ([string isEqualToString:[string stringByReplacingOccurrencesOfString:@"[^0-9-+]*" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, string.length)]]
            ? [super textField:textField shouldChangeCharactersInRange:range replacementString:string]
            : NO);
}

@end


@implementation DecimalFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return ([string isEqualToString:[string stringByReplacingOccurrencesOfString:@"[^0-9.]*" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, string.length)]]
            ? [super textField:textField shouldChangeCharactersInRange:range replacementString:string]
            : NO);
}

@end
